package com.spark.sniffer.tracing;

import java.util.UUID;

import com.spark.sniffer.constant.PassThroughEnum;

public class TraceId {
    private TraceId() {}

    public static final String KEY = PassThroughEnum.TRACEID.getValue();

    public static String generate() {
        return UUID.randomUUID().toString().substring(12).replace("-", "");
    }

    public static void putToContext(String traceid) {
        TracingContext.getContext().setTraceId(traceid).putPassThrough(TraceId.KEY, traceid);
    }

    public static void putToContext() {
        String traceid = generate();
        TracingContext.getContext().setTraceId(traceid).putPassThrough(TraceId.KEY, traceid);
    }

    public static void putToContextIfNotPresent() {
        if (TracingContext.getContext().getTraceId() == null) {
            return;
        }

        String traceid = generate();
        TracingContext.getContext().setTraceId(traceid).putPassThrough(TraceId.KEY, traceid);
    }
}
